<?php
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link
 * @package       Model
 * @since         Magic 1.0
 * @license
 */
?>
<?php
/**
 * @desc : This class is used to controll cache handling in to framework 
 * Basically its create clone html file of the view pages . 
 * At the time of the request comes if cache file is exist and it is into 
 * time limit ,this contoller class returns those fiels . 
 * Other wise it retuns actual file . Delete expier cache file for orignal file .
 * Write new cache file . 
 *   
 */
class MAGIC_CACHE { 
    
    /**
     * @desc : This is the variable which catch file name 
     */
    private $_filename ; 
    /**
     * @desc: This variable is used to catch the file content  
     * @var type 
     */
    private $_content ; 
    /**
     * @desc:This file extention static variable
     */
    private static $_ext='.html';
    /**
     *@desc:This is the instance variable of self
     * @var type 
     */
    private static $_instance;
    /**
     * @author : aNKIT kHAMBHATA
     * @desc: This function is used to give instance of class 
     */
    public static function callMethod() {
        self::$_instance = new self;
        return self::$_instance;
    }
    /**
     * @desc:This function is used to crete cache file name 
     * @return MAGIC_CACHE 
     */
    public function  cacheFile(){
        $this->_filename = CACHE_DIR.DS.Magic::callMethod()->getModel().Magic::callMethod()->getView().self::$_ext ; 
        return $this ;         
    }
    /**
     * @desc:This function is used to get content of the output file 
     * Which through the output to the browser 
     * @return type 
     */
    public function fileCatchContent(){
        ob_start();
        $content = serialize(ob_get_contents()) ; 
        ob_flush(); 
        ob_clean();
        $this->_content = $content ; 
        return $this  ; 
    }
    /**
     * @desc: This function is used to create cache file.
     * @param type $model:name of model
     * @param type $view : name of veiw file
     */
    
    public function createCacheFile() {
        if (file_exists($this->_filename)) {
            @unlink($file);
        }
        
        if (!file_put_contents($this->_filename, serialize( $this->_content))) {
                throw new Exception('Error writing data to cache file.');
        }
    }
    /**
     * @desc : This function is used to check 
     * cache file limit 
     * @return boolean true/false 
     */
    public function isCacheExpier(){
        $fileCacheTime = filemtime($this->_filename); 
        $diff = time()-$fileCacheTime ;
        $diff = date($diff,CACHE_UNIT); 
        if($diff < CACHE_LIMIT) {
            return true  ;  
        } else {
            return false ; 
        }
    }
}
?>
